<?php

namespace App\Modules\Admin\Services\Water\Member;

use App\Exceptions\Exception;
use App\Modules\Admin\Entities\Member\UserBankModel;
use App\Modules\Admin\Entities\Vip\VipModel;
use App\Modules\Admin\Services\BaseService;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;

class BankService extends BaseService
{
    public function __construct(BaseService $model)
    {
        $this->model = $model;
    }
    public function lists(array $params) : array
    {
        $params['where_callback'] = function($query) use ($params){
            $request = request();
            // 按照名称进行搜索
            if (!empty($search = $request->input('search', ''))){
                $query->where('nickname', 'LIKE', '%' . trim($search) . '%');
            }
            if (!empty($search = $request->input('search', ''))){
                $query->where('nickname', 'LIKE', '%' . trim($search) . '%');
            }
        };

        return parent::lists($params); // TODO: Change the autogenerated stub
    }

    /**
     * @param $uid
     * @return array
     */
    public function list($uid): array
    {
        $list = UserBankModel::where('user_id', $uid)
            ->get();
        $data = [];
        foreach ($list as $item) {
            $name = $item->name;
            $_name = Str::mask($name, '*', 0, Str::length($name) - 1);
            $data[] = [
                'id' => $item->id,
                'bank_info' => [
                    'icon' => Arr::get($item,'bank.icon',''),
                    'name' => Arr::get($item,'bank.name',''),
                ],
                'number' => substr($item->number,-4),
                'name' => $_name,
            ];
        }
        return $data;
    }

    /**
     * @param $uid
     * @param $id
     * @param $number
     * @param $name
     * @return true
     * @throws Exception
     */
    public function create(array $params)
    {

        return true;
    }

    /**
     * @param $id
     * @param $uid
     * @return array
     */
    public function find($id,$uid): array
    {
        $info = UserBankModel::where('user_id', $uid)->find($id);
        $name = Arr::get($info, 'name','');
        $_name = Str::mask($name, '*', 0, Str::length($name) - 1);
        return [
            'id' => Arr::get($info,'id',0),
            'bank_info' => [
                'icon' => Arr::get($info,'bank.icon',''),
                'name' => Arr::get($info,'bank.name',''),
            ],
            'number' => substr(Arr::get($info,'number'),-4),
            'name' => $_name,
        ];
    }

    /**
     * @param $id
     * @param $uid
     * @return true
     * @throws Exception
     */
    public function delete($id,$uid): true
    {
        $bool = UserBankModel::where('user_id', $uid)->where('id', $id)->delete();
        if(!$bool) {
            error('解绑出错，请重试');
        }
        return true;
    }
}
